<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\blog\model;
use think\Model;
use think\Db;
class PayModel extends Model
{

    /*
     * 支付
     * */
    public function pay($data,$where)
    {
         $orderid=$data['order'];
         $state=DB::name('order')->where(['orderNo'=>$orderid])->find();
         if($state['payStatus']==0||$state['payStatus']==-1)
         {
             $finance=$this->finance($data,$where);
             if($finance['sate']==1)
             {
                 $order=$this->order($data,$where,$finance['time']);
                 if($order==1) {
                     $con['sate'] = 1;
                     $con['con'] = '订单完成';
                     return $con;
                 }
             }else{
                 return $finance;
             }
         }else{
            $con['sate']=0;
            $con['con']='订单已经支付';
            return $con;
         }

    }
    /*
     * 参数的传递
     * 要对现金进行判断（未做）
     * */
    public function order($data,$cause,$time)
    {

        $where['orderNo']=$data['order'];
        $array['userid']=$cause['id'];
        $array['actualMoney']=$data['s_pay'];
        $array['cardid']=$cause['cardid'];
        $array['discount']=0;
        if($data['state']==1)
        {
            $array['getMoney']=0;
            $array['giveChange']=0;
        }elseif ($data['state']==2)
        {
            $array['getMoney']=$data['cash'];
            $array['giveChange']=$data['giveChange'];
        }

        $array['payType']=$data['state'];
        $array['payStatus']=1;
        $array['utime'] =$time;
        $state=DB::name('order')->where($where)->update($array);
        if($state)
        {
            $order_detail=$this->order_detail($data,$cause,$time);
            if($order_detail)
            {
                $con=1;
            }else{
                $con=0;
            }
            return $con;
        }else{
            $con=0;
            return $con;
        }
    }
    /*
     * 订单详情表
     * */
    public function order_detail($date,$cause,$time)
    {
       $where['orderid']=$date['order'];
       $data['paystatus']=1;
       $data['utime']=$time;
       $state=DB::name('order_detail')->where($where)->update($data);
       return $state;
    }
    /*
     * 余额表
     * */
    public function finance($data,$where)
    {
        $use=DB::name('finance')->where($where)->find();
        $new_oll=$use['money']-$data['s_pay'];
        $array['money']=$new_oll;
        $times=time();
        $state=DB::name('finance')->where($where)->update($array);
        if($state)
        {
            $time=$this->finance_record($data,$where,$times);
            if($time==1)
            {
                $con['sate']=1;
                $con['time']=$times;
                return $con;
            }else{
                $con['sate']=0;
                $con['time']=$times;
                return  $con;
            }
        }else{
            $con['sate']=0;
            $con['con']='不好意思现在余额支付不能使用';
            return  $con;
        }
    }
    /*
     * 余额流水表
     * */
    public function finance_record($date,$where,$time)
    {
        $data['userid']=$where['id'];
        $data['orderid']=$date['order'];
        $data['money']=$date['s_pay'];
        $data['storeid']=$where['storeid'];
        $data['ctime']=$time;
        $data['operatorid']=1;
        $data['status']=1;
        $state= DB::name('finance_record')->insert($data);
        if($state)
        {
             return $sta=1;
        }else{
            return  $sta=0;
        }
    }
    /*
     * 积分表
     */
    public function integral($method, $args)
    {
        return ;
    }
    /*
     * 积分流水表
     * */
    public function integral_record()
    {

    }
}